package com.ekalenchuk.module.controllers;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ekalenchuk.module.constants.PageConstants;
import com.ekalenchuk.module.domain.Article;
import com.ekalenchuk.module.service.ArticleService;

/**
 * Controller contains methods for processing requests to see pages with articles
 */
@Controller
public class ViewArticleController implements Serializable {
	
	private static final long serialVersionUID = 1533577656488477229L;
	
	public static final String ARTICLES = "articles";
	
	@Autowired
	private ArticleService articleService;
	
	
	/**
	 * Processes HTTP GET request. 
	 *
	 * @param model the model
	 * @return the string
	 */
	@RequestMapping(value="/articles", method=RequestMethod.GET)
	public String viewArticles(Model model) {
		
		prepareArticles(model);
		
		return PageConstants.ARTICLES;
	}
	
	
	/**
	 * Puts list of articles in DB into {@link Model}.
	 *
	 * @param model the model
	 */
	private void prepareArticles(final Model model) {
		List<Article> articles = articleService.getArticles();
		model.addAttribute(ARTICLES, articles);
	}
	

}
